<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.data" highlight-current-row :v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="菜单名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="url" label="菜单url" width="120" sortable>
      </el-table-column>
      <el-table-column prop="icon" label="icon" width="120" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="父级菜单" width="120" sortable>
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
                     :total="pageInfo.total"
                     style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增和编辑界面-->
    <el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="菜单名" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="菜单url" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="菜单图片" prop="icon">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>


        <el-form-item label="上级菜单">
          <el-cascader
              v-model="saveForm.parent"
              :options="menuTree"
              :props="{ expandTrigger: 'hover',
                         checkStrictly: true,
                         label:'name',
                         value:'id'
             }"
              clearable></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      pageInfo: {
        total: 0, // 总的数据条数
        data: [] // 列表数据
      }, // 分页数据
      query: {  // 查询分页
        currentPage: 1, // 当前页码
        pageSize: 10, // 查询条数
        keyword: '' // 关键字
      },
      menus: [],// 菜单列表
      menuTree: [],// 菜单树
      page: 1,
      listLoading: false, // 列表加载
      sels: [],//列表选中列
      saveFormVisible: false,//编辑新增界面是否显示
      saveLoading: false, // 编辑新增弹出框提交加载
      saveFormRules: {  //  编辑新增规则
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      saveForm: {}, //新增编辑界面数据
    }
  },
  methods: {
    // 获取菜单方法
    getParents() {
      axios.get("/menu").then(res => {
        if (res.status == 200) {
          // 查询成功
          this.menus = res.data.data
        }
      })
    },
    // 分页查询携带数据
    search() {
      this.query.currentPage = 1;
      this.getMenu();
    },
    // 监听当前是第几页，查询第几条数据
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getMenu();
    },
    //获取菜单列表
    getMenu() {
      axios.put("/menu", this.query).then(res => {
        if (res.status == 200) {
          // 查询成功
          this.$message.success("分页查询成功")
          this.pageInfo = res.data.data
        }
      }).catch(() => {
        this.$message.error("分页查询失败")
      })
    },
    //删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let id = row.id;
        // 删除方法
        this.deleteById(id)
      }).catch(() => {

      });
    },
    // 删除方法
    deleteById(id) {
      axios.delete("/menu/" + id).then(res => {
        if (res.status == 200) {
          // 关闭加载框
          this.listLoading = false;
          // 删除成功
          this.$message.success("删除成功")
        }
      }).catch(() => {
        // 删除失败
        this.$message.error("删除失败")
      }).finally(() => {
        this.getMenu()
      })
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
      // 上级部门树回显

      this.getParents()
      this.getDeptTree()
    },
    //显示新增界面
    handleAdd() {
      this.saveFormVisible = true;
      this.saveForm = {};
      this.getParents()
      this.getDeptTree()
    },
    //新增编辑
    saveSubmit() {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            // 提交新增编辑方法
            this.insertOrUpdate();
          });
        }
      });
    },
    // 新增修改
    insertOrUpdate() {
      console.log(this.saveForm)
      console.log(this.saveForm.parent)

      if (this.saveForm.parent) {
        this.saveForm.parent = {
          id: this.saveForm.parent[this.saveForm.parent.length - 1]
        }
      } else {
        this.saveForm.parent = {
          id: null
        }
      }


      if (this.saveForm.parent) {
        // 转为对象
        this.saveForm.parent = {...this.saveForm.parent}
      }

      axios.post("/menu", this.saveForm).then(res => {
        // 成功信息
        if (res.status == 200) {
          this.$message.success("保存成功")
        }
        // 关闭加载框
        this.saveLoading = false;
        // 关闭弹框
        this.saveFormVisible = false
      }).catch(() => {
        this.$message.error("保存失败")
      }).finally(() => {
        this.getMenu()
      })
    },
    // 列表选中列的变化
    selsChange(sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove() {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 批量删除
        this.batchDelete(ids);
      }).catch(() => {

      });
    },
    // 批量删除方法
    batchDelete(ids) {
      axios.patch("/menu", ids)
          .then(res => {
            if (res.status == 200) {
              // 批量删除成功
              this.$message.success("批量删除成功")
              // 关闭弹框
              this.listLoading = false;
              this.query.currentPage = 1
            }
          })
          .catch(() => {
            this.$message.error("批量删除失败")
          })
          .finally(() => {
            this.getMenu()
          })
    }
  },
  mounted() {
    this.getMenu();
  }
}

</script>

<style scoped>

</style>